#pragma once

#include <compl.h>
#include <dcont_arch.h>
/* 获取全局状态 */

/* try catch不需要额外栈，因为不能恢复 */

/* TODO: 这个还需要修改确保正确性(可以跟c++ exception对比) */

/* 需要有一个全局状态跟随 */
typedef struct{
	fresume_t	buf;
	word_t		retval;
}try_exc_t;

SAI try_exc_t **trycatch_get_pbuf();

/* TODO: 裸机需要实现这套接口 */
#define __TRY(excp)					\
	*trycatch_get_pbuf() = (excp);			\
	if (SETJMP(&((excp)->buf)) == 0)

#define DECL_TRYCATCH		try_exc_t __try_exc_point__; 

#define TRY_ONCE		DECL_TRYCATCH; __TRY(&__try_exc_point__)
#define TRY			__try_exc_point__.retval = 0; __TRY(&__try_exc_point__)

#define CATCH(exc_ret)		\
else if (__try_exc_point__.retval == (word_t)(exc_ret))

#define RAISE(exc_ret)		{\
	try_exc_t **pbuf = trycatch_get_pbuf();			\
	ASSUME(pbuf);						\
	try_exc_t *__raise_to = *pbuf;				\
	atm_st(&__raise_to->retval, (word_t)exc_ret);		\
	LONGJMP(&__raise_to->buf, 1);				\
}

/* thread local 不如改为设置依赖 */
#define DEF_UTRYCATCH(name, try_exec_pbuf)			\
SAI try_exc_t **trycatch_get_pbuf(){ return &try_exec_pbuf; } 
